﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PLAYXPERT.Windows;
using BackLib;

namespace SkypePXP
{
    public partial class SettingsPage : PXPForm
    {
        Settings settings;
        Hook hook;

        bool settingPTT = false;
        bool settingMTT = false;
        bool settingPTC = false;

        public SettingsPage(Settings settings, Hook hook)
        {
            InitializeComponent();
            this.settings = settings;
            this.hook = hook;
            this.hook.Toggle += new Hook.ToggleHandler(hook_Toggle);
            RefreshLabelText();
        }

        private void buttonClearPTT_Click(object sender, EventArgs e)
        {
            settings.keySettings.KeyPTT = "Not Set";
            RefreshLabelText();
        }

        private void buttonClearMTK_Click(object sender, EventArgs e)
        {
            settings.keySettings.KeyMTT = "Not Set";
            RefreshLabelText();
        }

        private void buttonClearSCK_Click(object sender, EventArgs e)
        {
            settings.keySettings.KeyPTC = "Not Set";
            RefreshLabelText();
        }

        private void buttonSetKeyPTT_Click(object sender, EventArgs e)
        {
            settingPTT = true;
            buttonSetKeyPTT.Text = "Press Key";
            buttonSetKeyPTT.Enabled = false;
            hook.settingKey = true;
        }

        private void buttonSetKeyMTT_Click(object sender, EventArgs e)
        {
            settingMTT = true;
            buttonSetKeyMTT.Text = "Press Key";
            buttonSetKeyMTT.Enabled = false;
            hook.settingKey = true;
        }

        private void buttonSetKeySCK_Click(object sender, EventArgs e)
        {
            settingPTC = true;
            buttonSetKeySCK.Text = "Press Key";
            buttonSetKeySCK.Enabled = false;
            hook.settingKey = true;
        }

        void hook_Toggle(bool Enabled, string Key)
        {
            if (settingPTT)
            {
                settings.keySettings.KeyPTT = Key;
                buttonSetKeyPTT.Text = "Set Key";
                buttonSetKeyPTT.Enabled = true;
                settingPTT = false;
                RefreshLabelText();
                settings.SaveSettings();
            }
            if (settingMTT)
            {
                settings.keySettings.KeyMTT = Key;
                buttonSetKeyMTT.Text = "Set Key";
                buttonSetKeyMTT.Enabled = true;
                settingMTT = false;
                RefreshLabelText();
                settings.SaveSettings();
            }
            if (settingPTC)
            {
                settings.keySettings.KeyPTC = Key;
                buttonSetKeySCK.Text = "Set Key";
                buttonSetKeySCK.Enabled = true;
                settingPTC = false;
                RefreshLabelText();
                settings.SaveSettings();
            }
        }

        void RefreshLabelText()
        {
            labelSetKeyPTT.Text = settings.keySettings.KeyPTT;
            labelSetKeyMTT.Text = settings.keySettings.KeyMTT;
            labelSetKeySCK.Text = settings.keySettings.KeyPTC;
            checkBoxHandleKeyPTT.Checked = settings.keySettings.HandlePTTKey;
            checkBoxHandleKeyMTT.Checked = settings.keySettings.HandleMTTKey;
            checkBoxHandleSCK.Checked = settings.keySettings.HandlePTCKey;
        }

        private void checkBoxHandleKeyPTT_CheckedChanged(object sender, EventArgs e)
        {
            settings.keySettings.HandlePTTKey = checkBoxHandleKeyPTT.Checked;
            settings.SaveSettings();
        }

        private void checkBoxHandleKeyMTK_CheckedChanged(object sender, EventArgs e)
        {
            settings.keySettings.HandleMTTKey = checkBoxHandleKeyMTT.Checked;
            settings.SaveSettings();
        }

        private void checkBoxHandleSCK_CheckedChanged(object sender, EventArgs e)
        {
            settings.keySettings.HandlePTCKey = checkBoxHandleSCK.Checked;
            settings.SaveSettings();
        }

        private void SettingsPage_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            settings.SaveSettings();
            this.Hide();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            settings.LoadSettings();
            RefreshLabelText();
            this.Hide();
        }

        private void panelSettingsPanel_Enter(object sender, EventArgs e)
        {
            RefreshLabelText();
        }

    }
}
